import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class DialogCreerCompte extends JDialog {
	private JTextField textFieldPseudo;
	private JTextField textFieldEmail;
	private JPasswordField passwordField;
	private Client controleur;
	
	public DialogCreerCompte(Client client) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				ouvrirDialogConnexion();
			}
		});
		this.controleur = client;
		this.setBounds(50,50,457,186);
		setTitle("Créer un Compte");
		getContentPane().setLayout(null);
		
		JLabel lblPseudo = new JLabel("Pseudo");
		lblPseudo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPseudo.setBounds(12, 12, 139, 15);
		getContentPane().add(lblPseudo);
		
		JLabel lblNewLabel = new JLabel("Mot de passe");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(12, 39, 139, 15);
		getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Email");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(12, 72, 139, 15);
		getContentPane().add(lblNewLabel_1);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(186, 118, 252, 27);
		getContentPane().add(splitPane);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ouvrirDialogConnexion();
			}
		});
		splitPane.setLeftComponent(btnAnnuler);
		
		JButton btnCreer = new JButton("Créer");
		btnCreer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				creerCompte();
			}

		});
		splitPane.setRightComponent(btnCreer);
		
		textFieldPseudo = new JTextField();
		textFieldPseudo.setBounds(169, 10, 269, 19);
		getContentPane().add(textFieldPseudo);
		textFieldPseudo.setColumns(10);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setColumns(10);
		textFieldEmail.setBounds(169, 70, 269, 19);
		getContentPane().add(textFieldEmail);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(169, 39, 269, 19);
		getContentPane().add(passwordField);
	}
	
	public void fermerFenetre()
	{
		this.controleur.fermerDialogCreerCompte();
	}
	
	public void ouvrirDialogConnexion()
	{
		this.controleur.ouvrirDialogConnexion();
		fermerFenetre();
	}
	
	private void creerCompte() {
		String pseudo = textFieldPseudo.getText();
		String email = textFieldEmail.getText();
		String password = new String(this.passwordField.getPassword());
		
		
		if (this.controleur.creerCompte(pseudo,password,email))
		{
			JOptionPane.showMessageDialog(this, "Le compte " + pseudo + " a bien été créé.", "Confirmation création compte", JOptionPane.INFORMATION_MESSAGE);
			this.controleur.fermerDialogCreerCompte();
			this.controleur.ouvrirDialogConnexion();
		}
		
		else 
		{
			JOptionPane.showMessageDialog(this, "Le compte " + pseudo + " n'a pas pu être créé.", "Erreur création compte", JOptionPane.ERROR_MESSAGE);
		}
		
	}
}
